﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;

public partial class Restricted_EditDateSite : System.Web.UI.Page
{
    private aspnet_User currentUser;
    private Datenight currentDatenight;
    private PersonBLL personBLL;
    private DatenightBLL datenightBLL;
    private CityBLL cityBLL;

    protected void Page_Load(object sender, EventArgs e)
    {
        
        personBLL = PersonBLL.Instance;
        datenightBLL = DatenightBLL.Instance;
        cityBLL = CityBLL.Instance;
        currentUser = personBLL.getCurrentUser(Membership.GetUser());
        currentDatenight = datenightBLL.findDatenightsByUsername(currentUser.UserName).Take(1).SingleOrDefault();

        if (!IsPostBack)
        {
            CalExtender.StartDate = DateTime.Today;
            CalExtender.EndDate = DateTime.Today.AddDays(14);

            if (currentUser.GenderId == 1)
            {
                DateGender.SelectedIndex = 2;
            }
            else if (currentUser.GenderId == 2)
            {
                DateGender.SelectedIndex = 1;
            }
            else
            {
                DateGender.SelectedIndex = 0;
            }

            FillAge();


            CityZipcodeTxt.Text = currentDatenight.City.Cityname + " (" + currentDatenight.City.Zipcode + ")";
            DateDate.Text = currentDatenight.Date.Date.ToString("dd MMM yyyy");
            DateGender.Text = currentDatenight.Gender.sex;
            DatePersonToPay.Text = currentDatenight.WhoPay;
            DateAgeFrom.Text = currentDatenight.Agemin.ToString();
            DateAgeTo.Text = currentDatenight.Agemax.ToString();
            DateDescription.Text = currentDatenight.Description;
        }
    }

    private void FillAge()
    {
        for (int i = 15; i <= 99; i++)
        {
            ListItem AgeFrom = new ListItem();
            AgeFrom.Text = i.ToString();
            DateAgeFrom.Items.Add(AgeFrom);
        }
        for (int i = 99; i >= 15; i--)
        {
            ListItem AgeTo = new ListItem();
            AgeTo.Text = i.ToString();
            DateAgeTo.Items.Add(AgeTo);
        }
    }

    //Method to get autocompletedate from database
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static string[] GetCompletionListCity(string prefixText, int count, string contextKey)
    {
        return DatenightBLL.Instance.getCitysForCompletionlist(prefixText).ToArray();
    }

    protected void UpdateDate_Click(object sender, EventArgs e)
    {
        DateiaftenDataContext db = new DateiaftenDataContext();
        var datenight2 = datenightBLL.findDatenightsByUsername(currentUser.UserName).Take(1).SingleOrDefault();

        //Extract city and zipnumber from textbox
        try
        {
            string cityname = CityZipcodeTxt.Text;
            cityname = cityname.Substring(0, cityname.Length - 7);
            string zipcode = CityZipcodeTxt.Text;
            zipcode = zipcode.Substring(zipcode.Length - 5, 4);
            var CityObj = cityBLL.FindCityByNameAndZipcode(cityname, zipcode);
            datenight2.City = CityObj;

            try
            {

                //Insert date from textbox on website
                CultureInfo provider = CultureInfo.InvariantCulture;
                DateTime date = DateTime.ParseExact(DateDate.Text, CalExtender.Format, provider);
                datenight2.Date = date;

                datenight2.Gender = personBLL.FindGenderById(DateGender.SelectedIndex);

                datenight2.WhoPay = DatePersonToPay.SelectedValue;
                datenight2.Agemin = Convert.ToInt32(DateAgeFrom.SelectedValue);
                datenight2.Agemax = Convert.ToInt32(DateAgeTo.SelectedValue);

                datenight2.Description = DateDescription.Text;
                datenightBLL.UpdateDatenight(datenight2);

                Page.Response.Redirect("~/Restricted/SearchSite.aspx?dateupdatesucces=true", false);
            }
            catch
            {
                WarningLabel.Text = "Datofelt er ikke udfyldt korrekt eller i forkert format";
            }
        }
        catch
        {
            WarningLabel.Text = "By/postnr felt er ikke gyldigt eller i forkert format";
        }
    }

    protected void DeleteDate_Click(object sender, EventArgs e)
    {
        try
        {
            datenightBLL.DeleteDatenight(currentDatenight);
            Page.Response.Redirect("~/Restricted/SearchSite.aspx?datedeletesucces=true", false);
        }
        catch
        {
            WarningLabel.Text = "Der skete en fejl og daten blev ikke slettet";
        }
    }
}